package com.szholly.utils.freemarker.directive;

import java.io.IOException;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import com.szholly.utils.spring.springmvc.route.FunctionInfo;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * UUID生成器
 */
@Component("directive_uuid")
public class UUIDDirective implements TemplateDirectiveModel, IDirective {

	@SuppressWarnings("rawtypes")
	@Override
	public void execute(Environment env, Map arg1, TemplateModel[] arg2,
			TemplateDirectiveBody arg3) throws TemplateException, IOException {
		String uuid = UUID.randomUUID().toString();
		uuid = StringUtils.remove(uuid, '-');
		env.getOut().append(uuid);
	}
	
	@Override
	public FunctionInfo getFunctionInfo() {
		String paraInfo = "示例：<@uuid />";
		FunctionInfo info = new FunctionInfo("uuid", 1, "ͨ通用",
				"UUID生成器", paraInfo);
		return info;
	}
}
